from flaskapp.exts import db


class PsnlRecord(db.Model):
    __tablename__ = 'Psnlbooksrec'
    recordid = db.Column(db.Integer, primary_key=True)
    bookid = db.Column(db.Integer, nullable=False)
    loanerid = db.Column(db.Integer, nullable=False)
    borrowerid = db.Column(db.Integer, nullable=False)
    startdate = db.Column(db.DateTime, nullable=False)
    loandays = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return f'<Record {self.recordid}, {self.bookid},{self.loanerid}, {self.borrowerid},{self.startdate}, {self.loandays},>'

    def __init__(self, bookid, loanerid, borrowerid, startdate, loandays):
        self.bookid = bookid
        self.loanerid = loanerid
        self.borrowerid = borrowerid
        self.startdate = startdate
        self.loandays = loandays

    def to_dict(self):
        return {
            'recordid': self.recordid,
            'bookid': self.bookid,
            'borrowerid': self.borrower,
            'loanerid': self.loanerid,
            'startdate': self.startdate,
            'loandays': self.loandays,

        }

    def addRecord(self):
        db.session.add(self)
        db.session.commit()

    def deleteRecord(self):
        db.session.delete(self)
        db.session.commit()
